package com.wisdytech.linkdcs.log.action;

import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.bean.StandardTableResult;
import com.wisdytech.common.handler.ResourceMatchingService;
import com.wisdytech.common.utils.DataTransformUtil;
import com.wisdytech.common.utils.ExcelUtil;
import com.wisdytech.linkdcs.log.dto.LogSearchDTO;
import com.wisdytech.linkdcs.log.model.SysLog;
import com.wisdytech.linkdcs.log.service.ISysLogService;
import com.wisdytech.linkdcs.log.vo.SysLogVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 
 * <p>ClassName: ReportAction</p>
 * <p>Description: 报表统计action</p>
 * <p>Author: dailibo</p>
 * <p>Date: 2017-10-13</p>
 */
@Controller
@RequestMapping("/log")
public class LogAction {
    
    @Resource
    private ISysLogService sysLogService;
    @Autowired
    @Qualifier("resourceMatchingService")
    private ResourceMatchingService resourceMatchingService;
    @RequestMapping("/access/index")
    public String access(Model model,String emModel, HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
        if(StringUtils.isNotBlank(emModel)){
            model.addAttribute("emModel",emModel);
        }
        return "log/access";
    }
    
    @RequestMapping("/error/index")
    public String error(Model model,String emModel, HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
        if(StringUtils.isNotBlank(emModel)){
            model.addAttribute("emModel",emModel);
        }
        return "log/error";
    }
    
    @RequestMapping("/operate/index")
    public String operate(Model model,String emModel, HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
        if(StringUtils.isNotBlank(emModel)){
            model.addAttribute("emModel",emModel);
        }
        return "log/operate";
    }
    
    @RequestMapping("/listLogs/get.mvc")
    @ResponseBody
    public StandardTableResult<SysLogVO> getSysLogs(Filter filter ,LogSearchDTO search) throws IOException{
      //通过查询条件查询所有日志信息并分页
        PageInfo<SysLog> sysLogs = sysLogService.listSysUsers(search, filter);
        //转换为前台结果集
        List<SysLog> sysLogList = sysLogs.getList();
        List<SysLogVO> logVOList=new ArrayList<SysLogVO>();
        for(SysLog sysLog: sysLogList){
            logVOList.add(DataTransformUtil.changeSysLogVO(sysLog));
        }
        return new StandardTableResult<SysLogVO>(sysLogs.getTotal(),logVOList);
    }
    
    /**
     * <p>Description: 导出用户信息</p>
     * @param request 请求对象
     * @param response 返回对象
     * @throws Exception 异常
     */
    @RequestMapping(value = "/exportOperate/export.mvc")
    @ResponseBody
    public void exportData(HttpServletRequest request, HttpServletResponse response, Filter filter) throws Exception {
        SimpleDateFormat dateFormat;
        dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String[] columnNames = null;
        String[] fileNames = null;
        String fileName = "操作日志";
        String userName = request.getParameter("userName");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        String module = request.getParameter("module");
        String userIp = request.getParameter("userIp");
        String content = request.getParameter("content");
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("content", content);
        queryMap.put("startTime", startTime);
        queryMap.put("endTime", endTime);
        queryMap.put("module", module);
        queryMap.put("userIp", userIp);
        queryMap.put("userName", userName);
        filter.setSize(65535);
        PageInfo<SysLog> sysLogs = sysLogService.listLogs(queryMap, filter);
        List<SysLog> logList = sysLogs.getList();
        String finalFileName = null;  
        final String userAgent = request.getHeader("USER-AGENT"); 
        if(StringUtils.contains(userAgent, "MSIE")){
            //IE浏览器  
            finalFileName = URLEncoder.encode(fileName,"UTF8");  
        }else {
            finalFileName = fileName;
        }
        columnNames = new String[] { "操作时间", "操作用户", "用户IP", "模块名称", "日志内容" };
        fileNames = new String[] { "createDate", "userName", "routeIp", "module", "remark" };
        ExcelUtil.exportExcel(response, logList, SysLog.class, columnNames, fileNames,
                finalFileName + dateFormat.format(new Date()));
    }

    /**
     * 查询错误详情
     * @return
     */
    @RequestMapping("/showDetail/get.mvc")
    @ResponseBody
    public StandardResult showDetail(String pkId) {
        StandardResult result = new StandardResult(true);
        String detail = sysLogService.getErrorLogDetail(pkId);
        result.setData(detail);
        return result;
    }
    
    

}
